package com.utils
{
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.media.SoundLoaderContext;
	import flash.net.URLRequest;
	import flash.events.*;
	import mx.controls.Alert;
	
	
	public class RadioStream extends EventDispatcher {
		
		public var s:Sound = new Sound();
		public var channel:SoundChannel;
		public var trans:SoundTransform = new SoundTransform(1, 0);
		//public var context:SoundLoaderContext = new SoundLoaderContext(5000, true);
		public var req:URLRequest;
		public var isLoaded:Boolean = false;
		public var isPlaying:Boolean = false;
		public var isReadyToPlay:Boolean = false;
		
		/**
		 * Constructor.
		 */
		
		public function RadioStream(soundUrl:String):void
		{
			req = new URLRequest(soundUrl);
		}
		
		private function completeHandler(event:Event):void {
			Alert.show("completeHandler: " + event);
		}
		
		private function id3Handler(event:Event):void {
			Alert.show("id3Handler: " + event);
		}
		
		private function ioErrorHandler(event:Event):void {
			Alert.show("ioErrorHandler: " + event);
			this.dispatchEvent(event.clone());
		}
		
		private function progressHandler(event:ProgressEvent):void {
			//Alert.show("progressHandler: " + event);
		}
		
		public function toggleStream():void {
			if(this.isPlaying) {
				this.stop();				
			}
			else {
				this.play();
			}
		}
		
		public function load():void {
			if (this.isPlaying) {
				this.stop();
				this.s.close();
			}
			this.isLoaded = false;
			
			this.s = new Sound();
			
			//this.s.addEventListener(Event.COMPLETE, completeHandler);
			//this.s.addEventListener(Event.ID3, id3Handler);
			this.s.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			this.s.addEventListener(Event.OPEN, onLoadOpen);
			this.s.addEventListener(Event.OPEN, onLoadOpen);
			this.s.addEventListener(SecurityErrorEvent.SECURITY_ERROR, ioErrorHandler);
			//this.s.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			
			var context:SoundLoaderContext = new SoundLoaderContext(5000, true);
			this.s.load(req, context);
		}
		
		public function onLoadOpen(event:Event):void {
			this.isReadyToPlay = true;
			//this.channel = s.play(0, 0, trans);
			this.play();
			this.dispatchEvent(event.clone());
		}
		
		public function play():void {
			if (!this.isPlaying)
			{
				if (this.isReadyToPlay)
				{
					this.channel = this.s.play(0, 0, trans);
					//this.sc.addEventListener(Event.SOUND_COMPLETE, onPlayComplete);
					this.isPlaying = true;
				}
				else if (!this.isLoaded)
				{
					// start loading again and play when ready
					// it appears to resume loading from the spot where it left off...cool
					this.load();
					return;
				}
			} 
		}
		
		public function stop():void {
			if (this.isPlaying) {
				this.channel.stop();
				this.isPlaying = false;
			}
			if (!this.isLoaded)
			{
				// stop streaming
				this.s.close();
				this.isReadyToPlay = false;
			}
		}
	}
	
}